// src/utils/request.js
import http from '../components/firstui/fui-request'
// export const AxiosUrl = "http://172.20.10.3:8080/shangrao"
export const AxiosUrl = "http://10.180.155.171:8080/shangrao"

import message from '@/utils/message'

// 初始化请求配置项
http.create({
  // 接口域名
  host: AxiosUrl,
  // host: 'http://192.168.110.104:8080/shangrao',
  header: {
    // 'content-type': 'application/x-www-form-urlencoded'
  }
})

// 请求拦截
http.interceptors.request.use(config => {
	console.log(config)
	if(typeof config.data != 'undefined'){
		let isParams = config.data
		let result = {}
		Object.keys(isParams).forEach(item=>{
			if(isParams[item] !== "" && isParams[item] != null){
				result[item] = isParams[item]
			}
		})
		config.data = result	
	}
  // 请求之前可在请求头中加入token等信息
  let token = uni.getStorageSync('uniapp_token') || 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJzeXNfdXNlcjoxIiwicm5TdHIiOiIycG5tcmNrTmx4Y1BDc09OS1lyb3dYZzJvNnhsaUVhSSIsInVzZXJJZCI6MX0.2OnzI7AAGZQy-Jsyy2nXCeJMaOU9xxPvZ16R4Kbj6MY'
  if (config.header) {
    config.header['token'] = token
  } else {
    config.header = {
      'authorization': token
    }
  }
  return config
})

// 响应拦截
http.interceptors.response.use(response => {
	if((typeof response.data.code =="undefined" || response.data.code != 200)){//报错提示
	 if(response.data.code == 401){
		 uni.redirectTo({
		 	url: `/pages/login/login`
		 });
	 }else{
		message({message:response.data.msg||'未知错误,请联系管理员!',type: 'error',duration: 3000,position: 'top'})
	 }
	}
  // TODO
  return response.data
}, (err) => {
  // 错误响应：断网、错误域名等
  return Promise.reject(err)
})

export default http